# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import json
import codecs
import sys
import pymongo

class ZhihuspiderFilePipeline(object):
    def __init__(self):
        self.file = codecs.open('zhihu_spider_file.json', mode='wb',encoding='utf-8')

    def process_item(self, item, spider):
        line = 'the new movie list:'+'\n'
        
        if item:
            user_name = items['user_name']
            user_gender = items['user_gender']
            user_location = items['user_location']
            user_followees = items['user_followees']
            user_followers = items['user_followers']
            user_be_aggred = items['user_be_aggred']
            user_be_thanked = items['user_be_thanked']
            user_education_school = items['user_education_school']
            user_education_subject = items['user_education_subject']
            user_employment = items['user_employment']
            user_employment_extra = items['user_employment_extra']
            user_info = items['user_info']
            user_intro = items['user_intro']

            line += json.dumps(user_name, ensure_asicii=False)
            line += json.dumps(user_gender, ensure_asicii=False)
            line += json.dumps(user_location, ensure_asicii=False)
            line += json.dumps(user_followees, ensure_asicii=False)
            line += json.dumps(user_followers, ensure_asicii=False)
            line += json.dumps(user_be_aggred, ensure_asicii=False)
            line += json.dumps(user_be_thanked, ensure_asicii=False)
            line += json.dumps(user_education_school, ensure_asicii=False)
            line += json.dumps(user_education_subject, ensure_asicii=False)
            line += json.dumps(user_employment, ensure_asicii=False)
            line += json.dumps(user_employment_extra, ensure_asicii=False)
            line += json.dumps(user_info, ensure_asicii=False)
            line += json.dumps(user_intro, ensure_asicii=False)
            
        self.file.write(line)


    def close_spider(self,spider):

        self.file.close()


class ZhihuspiderMongoPipeline(object):

    def __init__(self):
        self.mongo_url = 'localhost'
        self.mongo_port = 27017
        self.collection_name = 'zhihu_category'
        self.db_name = 'zhihu_spider'

    def open_spider(self,spider):
        self.client = pymongo.MongoClient(self.mongo_url, self.mongo_port)
        self.db = self.client[self.db_name]
        self.con = self.db[self.collection_name]

    def process_item(self,items,spider):
        if items:
            user_name = items['user_name']
            user_gender = items['user_gender']
            user_location = items['user_location']
            user_followees = items['user_followees']
            user_followers = items['user_followers']
            user_be_aggred = items['user_be_aggred']
            user_be_thanked = items['user_be_thanked']
            user_education_school = items['user_education_school']
            user_education_subject = items['user_education_subject']
            user_employment = items['user_employment']
            user_employment_extra = items['user_employment_extra']
            user_info = items['user_info']
            user_intro = items['user_intro']
            user_dict = {
                'user_name' : user_name,
                'user_gender' : user_gender,
                'user_location' : user_location,
                'user_followees' : user_followees,
                'user_followers' : user_followers,
                'user_be_aggred' : user_be_aggred,
                'user_be_thanked' : user_be_thanked,
                'user_education_school' : user_education_school,
                'user_education_subject' : user_education_subject,
                'user_employment' : user_employment,
                'user_employment_extra' : user_employment_extra,
                'user_info' : user_info,
                'user_intro' : user_intro
            }
            self.con.insert_one(user_dict)
            print user_name

    def close_spider(self,spider):
        self.client.close()
